package com.lzx.applib.widget.image.imageview_attacher;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.Scroller;


public	class MGestureListener implements OnGestureListener {

	ImageViewAttacher attacher;
	Context context;
	FlingRunnable mFlingRunnable;
	
	public MGestureListener(Context context, ImageViewAttacher attacher) {
		// TODO Auto-generated constructor stub
		this.attacher = attacher;
		this.context = context;
		this.mFlingRunnable = new FlingRunnable();
	}
	
	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		attacher.log("OnGestureListener : onDown");
		mFlingRunnable.endFling();
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		attacher.log("OnGestureListener : onShowPress");
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		attacher.log("OnGestureListener : onSingleTapUp");
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		attacher.log("OnGestureListener : onScroll" + "__distanceX:" + distanceX
				+ ";distanceY:" + distanceY);
		attacher.move((int)distanceX, (int)distanceY);
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		attacher.log("OnGestureListener : onLongPress");
		attacher.onLongPress();
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		attacher.log("OnGestureListener : onFling " + "velocityX:" + velocityX
				+ "; velocityY:" + velocityY);
		mFlingRunnable.start((int) velocityX, (int) velocityY);
		return false;
	}

	
	private class FlingRunnable implements Runnable {

		private static final int TOUCH_MODE_IDLE = 0;
		private static final int TOUCH_MODE_SCROLLING = 1;
		private static final int TOUCH_MODE_FLINGING = 2;
		private static final int TOUCH_MODE_DOWN = 3;
		private static final int TOUCH_MODE_TAP = 4;
		private static final int TOUCH_MODE_DONE_WAITING = 5;

		final Scroller mScroller;
		int mTouchMode;

		int mLastFlingY;
		int mLastFlingX;

		FlingRunnable() {
			mScroller = new Scroller(context);
		}

		void start(int initialVelocityX, int initialVelocityY) {
			int initialX = initialVelocityX < 0 ? Integer.MAX_VALUE : 0;
			int initialY = initialVelocityY < 0 ? Integer.MAX_VALUE : 0;
			
			mLastFlingX = initialX;
			mLastFlingY = initialY;
			
			mScroller.forceFinished(true);
			
			mScroller.fling(initialX, initialY, 
								initialVelocityX, initialVelocityY, 
								0, Integer.MAX_VALUE, 
								0, Integer.MAX_VALUE);
			
			mTouchMode = TOUCH_MODE_FLINGING;
			postOnAnimate(this);
		}

		void startScroll(int distance, int duration) {
			int initialY = distance < 0 ? Integer.MAX_VALUE : 0;
			mLastFlingY = initialY;
			mScroller.startScroll(0, initialY, 0, distance, duration);
			mTouchMode = TOUCH_MODE_FLINGING;
			postOnAnimate(this);
		}

		void endFling() {
			mLastFlingY = 0;
			mTouchMode = TOUCH_MODE_IDLE;

			attacher.img.removeCallbacks(this);

			mScroller.forceFinished(true);
		}

		public void run() {
			switch (mTouchMode) {
			case TOUCH_MODE_FLINGING: 
				final Scroller scroller = mScroller;
				boolean more = scroller.computeScrollOffset();
				final int x = scroller.getCurrX();
				final int y = scroller.getCurrY();

				int deltaX = mLastFlingX - x;
				int deltaY = mLastFlingY - y;


				if (more) {
					attacher.move(deltaX, deltaY);
					
					mLastFlingX = x;
					mLastFlingY = y;
					postOnAnimate(this);
				} else {
					endFling();
				}
				break;
			default:
				return;
			}
		}
		private void postOnAnimate(Runnable runnable) {
	        ViewCompat.postOnAnimation(attacher.img, runnable);
	    }
	}

}
	